<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <meta name="geometry" content="area">
  <link rel="stylesheet" href="./assets/common.css">
  <title>Stream Graph</title>
</head>

<body>
  <div id="canvas"></div>
  <script src="./assets/lodash-4.17.4.min.js"></script>
  <script src="./assets/jquery-3.2.1.min.js"></script>
  <script src="./assets/data-set.min.js"></script>
  <script src="../build/g2.js"></script>
  <script>
    // const t0 = Date.now();
    $.getJSON('./data/baby-names.json', data => {
      const ds = new DataSet();
      const dv = ds.createView('demo').source(data)
        .transform({
          type: 'fill-rows',
          groupBy: [ 'name' ],
          orderBy: [ 'year' ]
        })
        .transform({
          type: 'impute',
          field: 'n',
          method: 'value',
          value: 0
        })
        .transform({
          type: 'aggregate',
          fields: [ 'n' ],
          operations: [ 'sum' ],
          groupBy: [ 'year', 'name' ],
          orderBy: [ 'year' ],
          as: [ 'count' ]
        });

      const chart = new G2.Chart({
        container: 'canvas',
        forceFit: true,
        height: window.innerHeight,
        padding: [ 20, 180, 50, 50 ],
        plotBackground: {
          stroke: '#ccc'
        }
      });
      chart.source(dv, {
        year: {
          tickInterval: 10,
          nice: false
        },
        count: {
          nice: false
        }
      });
      chart.legend({
        useHtml: true,
        flipPage: true,
        position: 'right',
        title: {
          text: '图例可滚动'
        },
        'g2-legend-marker': {
          borderRadius: 'none'
        },
        'g2-legend-title': {
          fontSize: '12px',
          fontWeight: 500,
          margin: 0,
          color: '#ff8800'
        }
      });
      chart.tooltip({
        shared: false,
        crosshairs: false
      });
      chart.axis('year', {
        title: null,
        line: null,
        tickLine: null
      });
      chart.axis('count', {
        title: null,
        line: null,
        tickLine: {
          length: 8
        },
        subTickCount: 10,
        subTickLine: {
          lineWidth: 1, // 子刻度线宽
          stroke: '#ddd', // 子刻度线颜色
          length: 5
        },
        grid: null
      });
      chart.area()
        .position('year*count')
        .adjust([ 'stack', 'symmetric' ])
        .color('name')
        .shape('smooth')
        .opacity(1);
      chart.render();
    });
  </script>
</body>

</html>
